package tests;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.rmi.RemoteException;
import java.rmi.registry.Registry;
import java.util.Date;
import java.util.Random;

import pa2.*;

public class TestCase1 {
	String host = "localhost";
	String port = Integer.toString(Registry.REGISTRY_PORT);
	IMyKVPeer[] peers = null;

	String file = "testCase.csv";
	FileWriter fwriter = null;

	private long startTime = 0;
	private long endTime = 0;
	private long time = 0;

	public TestCase1(IMyKVPeer[] peers) {
		this.peers = peers;
	}

	public void startTest() {
		Random r = new Random();
		// lookup for a random key when the data structure is empty
		long key = r.nextInt((int) IMyKVPeer.MAX_KEY_VALUE);
		MyKVMessage msg = new MyKVMessage(key, Long.toHexString(key));
		try {
			this.startTime = new Date().getTime();
			String lookupString = peers[(int) key % IMyKVPeer.NUM_OF_PEERS].lookup(msg);
			this.endTime = new Date().getTime();
			time = this.endTime - this.startTime;

			System.out.println("\n(TestCase1) Looking up (empty)'" + msg.getKey() + "': "
					+ "returned value = " + lookupString + " in " + time + " ms\n");
		} catch (RemoteException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		File docu = new File(file);
		try {
			fwriter = new FileWriter(docu, true);
			fwriter.write("\n(TestCase1) Lookup (empty):;" + Long.toString(time) + "\n");
			fwriter.flush();
			fwriter.close();
		} catch (IOException e) {
			System.out
					.println("Upps... Problem beim Schreiben in \"testCases.csv\"!");
			e.printStackTrace();
		}

	}

}
